/**
 * Copyright 2016 JustWayward Team
 * <p>
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * <p>
 * http://www.apache.org/licenses/LICENSE-2.0
 * <p>
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.hzf.booklibrary.api;

import com.hzf.booklibrary.base.Constant;
import com.hzf.booklibrary.bean.AutoComplete;
import com.hzf.booklibrary.bean.BookClassify;
import com.hzf.booklibrary.bean.BookSource;
import com.hzf.booklibrary.bean.BookToc;
import com.hzf.booklibrary.bean.ChapterRead;
import com.hzf.booklibrary.bean.HotWord;
import com.hzf.booklibrary.bean.Recommend;
import com.hzf.booklibrary.bean.RecommendBookList;
import com.hzf.booklibrary.utils.LogUtils;


import java.util.List;

import okhttp3.OkHttpClient;
import retrofit2.Retrofit;
import retrofit2.adapter.rxjava.RxJavaCallAdapterFactory;
import retrofit2.converter.gson.GsonConverterFactory;
import retrofit2.http.Query;
import rx.Observable;

/**
 * https://github.com/JustWayward/BookReader
 *
 * @author yuyh.
 * @date 2016/8/3.
 */
public class BookApi {

    public static BookApi instance;

    private BookApiService service;

    public BookApi(OkHttpClient okHttpClient) {
        //联网请求
        Retrofit retrofit = new Retrofit.Builder()
                .baseUrl(Constant.BASE_BOOK_URL)
                .addCallAdapterFactory(RxJavaCallAdapterFactory.create()) // 添加Rx适配器
                .addConverterFactory(GsonConverterFactory.create()) // 添加Gson转换器
                .client(okHttpClient)
                .build();
        LogUtils.e(Constant.API_BASE_URL);
        service = retrofit.create(BookApiService.class);

    }

    public static BookApi getInstance(OkHttpClient okHttpClient) {
        if (instance == null)
            instance = new BookApi(okHttpClient);
        return instance;
    }
    public Observable<BookClassify> getBookClassify() {
        return service.getBookClassify();
    }

    public Observable<Recommend> getRecommend(String page,String classify,int id) {
        if(classify.equals("推荐")){
            return service.getRecomend(page,"20");
        }else{
            return service.getBooksByClassifid(page,"20",id+"");
        }
    }

    public Observable<Recommend> booksrecommendListByClassId( Integer classId){
        return service.booksrecommendListByClassId(classId);
    }

    public Observable<Recommend> booksrecommendListByPage( Integer page, Integer pageSize, Integer classId ){
        return service.booksrecommendListByPage(page,pageSize,classId);
    }

    public Observable<BookToc> getBookToc(String bookId, String view) {
        return service.getBookToc(bookId, view);
    }
    public synchronized Observable<ChapterRead> getChapterRead(String url) {
        return service.getChapterRead(url);}

    public Observable<HotWord> getHotWord() {
        return service.getHotWord();
    }
    public Observable<Recommend> getSearchResult(String page,String key) {
        return service.searchBooks(page,"20",key);
    }

    public synchronized Observable<List<BookSource>> getBookSource(String view, String book) {
        return service.getBookSource(view, book);
    }

    public Observable<AutoComplete> getAutoComplete(String query) {
        return service.autoComplete(query);
    }

    public Observable<RecommendBookList> getRecommendBookList(String bookId, String limit) {
        return service.getRecommendBookList(bookId, limit);
    }
}
